<?php
/**
 * Created by PhpStorm.
 * User: linye
 * Date: 2018/11/15
 * Time: 13:55
 */

namespace app\admin\controller\user;

use app\admin\model\AdminLog;
use app\common\controller\Backend;

class Level extends Backend {
    protected $user = null;
    protected $model=null;
    public function _initialize()
    {
        parent::_initialize();
        $this->user = model('User');
        $this->model=model('level');
    }
    /*
     * 查看等级排行榜
     */
    public function index(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where , $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->user
                ->where($where)
                ->order("level", $order)
                ->count();
            $list = $this->user
                ->where($where)
                ->order("level", $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v)
            {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);

        }
        return $this->view->fetch();
    }
    /*
    * 查看等级排行榜
    */
    public function manage(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where , $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->user
                ->where($where)
                ->order("create", $order)
                ->count();
            $list = $this->user
                ->where($where)
                ->order("level", $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v)
            {
                $v->hidden(['create', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    public function level(){

    }
    /**
     * 新增和更新会员等级信息
     * @return \think\response\Json
     */
    public function saveOrUpdate(){
        $id=$this->request->request('id');
        $data=['levelname'=>$this->request->request('levelname'),
            'level'=>$this->request->request('level'),
            'discount'=>$this->request->request('discount'),
            'upcondition'=>$this->request->request('upcondition'),
            'status'=>$this->request->request('status')];
        if($id!=null&&!empty($id)&&$id!=''){
            $data['updatetime']=time();
            $data['updaterid']=$this->request->session()['admin']['id'];
            $data['updater']=$this->request->session()['admin']['username'];
            $result=$this->model->where('id',$id)
                ->update($data);
            AdminLog::record('编辑会员等级');
            //AdminLog::record('编辑会员等级');
        }else{
            $id=uniqid();
            $data['id']=$id;
            $data['createtime']=$this->time();
            $data['createrid']=$this->request->session()['admin']['id'];
            $data['creater']=$this->request->session()['admin']['nickname'];
            $result=$this->model->insert($data);
            AdminLog::record('新增会员等级');
        }
        return json($result);
    }

}